from __future__ import annotations

import sys

from PyQt5 import QtCore, QtGui, QtWidgets

from services.registration_manager import RegistrationManager
from ui.component_dialog import ComponentOperationsDialog
from ui.pointcloud_dialog import PointCloudDialog
from ui.viewer import ViewerApp, load_groups_with_manager


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self) -> None:
        super().__init__()
        self.setWindowTitle("高级 OBJ 组件分割管理器")
        self.setGeometry(100, 100, 800, 600)

        app_icon = QtGui.QIcon("1.ico")
        self.setWindowIcon(app_icon)

        self.obj_data = None
        self.groups = []
        self.registration_manager = RegistrationManager()

        self._create_ui()
        self._apply_dark_theme()

    def _apply_dark_theme(self) -> None:
        dark_theme = """
            QMainWindow, QDialog, QWidget {
                background-color: #2D2D2D;
                color: #E0E0E0;
            }
            QLabel {
                color: #FFFFFF;
            }
            QPushButton {
                background-color: #505050;
                border: 1px solid #606060;
                border-radius: 8px;
                padding: 15px;
                font-size: 16px;
                color: #FFFFFF;
            }
            QPushButton:hover {
                background-color: #606060;
            }
            QPushButton:pressed {
                background-color: #404040;
            }
            QFrame {
                background-color: #353535;
                border: 1px solid #404040;
                border-radius: 5px;
            }
            QGroupBox {
                background-color: #353535;
                border: 1px solid #404040;
                border-radius: 5px;
                color: #FFFFFF;
                font-weight: bold;
            }
            QGroupBox::title {
                color: #FFFFFF;
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 3px;
            }
        """
        self.setStyleSheet(dark_theme)

    def _create_ui(self) -> None:
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        main_layout = QtWidgets.QVBoxLayout(central_widget)
        main_layout.setContentsMargins(20, 20, 20, 20)
        main_layout.setSpacing(20)

        title_label = QtWidgets.QLabel("OBJ 组件管理系统")
        title_label.setStyleSheet("font-size: 24px; font-weight: bold; color: #FFFFFF;")
        title_label.setAlignment(QtCore.Qt.AlignCenter)
        main_layout.addWidget(title_label)

        button_frame = QtWidgets.QFrame()
        button_layout = QtWidgets.QVBoxLayout(button_frame)
        button_layout.setContentsMargins(40, 40, 40, 40)
        button_layout.setSpacing(30)

        btn_component_ops = QtWidgets.QPushButton("组件操作")
        btn_component_ops.setMinimumHeight(70)
        btn_component_ops.clicked.connect(self.open_component_operations)
        button_layout.addWidget(btn_component_ops)

        btn_pointcloud = QtWidgets.QPushButton("转点云")
        btn_pointcloud.setMinimumHeight(70)
        btn_pointcloud.clicked.connect(self.show_pointcloud_dialog)
        button_layout.addWidget(btn_pointcloud)

        btn_preassemble = QtWidgets.QPushButton("预拼装")
        btn_preassemble.setMinimumHeight(70)
        btn_preassemble.clicked.connect(self.show_point_cloud_match_dialog)
        button_layout.addWidget(btn_preassemble)

        main_layout.addWidget(button_frame)

        footer_label = QtWidgets.QLabel("请选择要执行的操作")
        footer_label.setStyleSheet("font-size: 14px; color: #AAAAAA;")
        footer_label.setAlignment(QtCore.Qt.AlignCenter)
        main_layout.addWidget(footer_label)

        version_label = QtWidgets.QLabel("版本 1.0")
        version_label.setStyleSheet("font-size: 12px; color: #888888;")
        version_label.setAlignment(QtCore.Qt.AlignCenter)
        main_layout.addWidget(version_label)

    # ------------------------------------------------------------------ #
    # Button handlers
    # ------------------------------------------------------------------ #
    def open_component_operations(self) -> None:
        dialog = ComponentOperationsDialog(self, self.obj_data, self.groups)
        if dialog.exec_() == QtWidgets.QDialog.Accepted:
            self.obj_data = dialog.obj_data
            self.groups = dialog.groups

    def show_pointcloud_dialog(self) -> None:
        dialog = PointCloudDialog(self, self.obj_data, self.groups)
        dialog.exec_()

    def show_point_cloud_match_dialog(self) -> None:
        groups = load_groups_with_manager(self.registration_manager)
        if not groups:
            QtWidgets.QMessageBox.warning(self, "提示", "未找到可用的点云组，请先执行配准或检查数据。")
            return
        dialog = ViewerApp(groups, self, registration_manager=self.registration_manager)
        dialog.exec_()


def run() -> None:
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("Fusion")
    app.setStyleSheet(
        """
        QMainWindow, QDialog, QWidget {
            background-color: #2D2D2D;
            color: #E0E0E0;
        }
        """
    )

    window = MainWindow()
    window.show()
    sys.exit(app.exec_())